package com.ruoyi.exam.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.exam.domain.vo.QuestionVo;
import com.ruoyi.system.domain.TTextContent;
import com.ruoyi.system.mapper.TTextContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.exam.mapper.TQuestionMapper;
import com.ruoyi.exam.domain.TQuestion;
import com.ruoyi.exam.service.ITQuestionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 题库Service业务层处理
 *
 * @author Bandit
 * @date 2022-11-01
 */
@Service
public class TQuestionServiceImpl implements ITQuestionService
{
    @Autowired
    private TQuestionMapper tQuestionMapper;

    @Autowired
    private TTextContentMapper contentMapper;

    /**
     * 查询题库
     *
     * @param id 题库主键
     * @return 题库
     */
    @Override
    public QuestionVo selectTQuestionById(Long id)
    {
        QuestionVo questionVo = new QuestionVo();
        // 获取试题
        TQuestion tQuestion = tQuestionMapper.selectTQuestionById(id);
        if (tQuestion == null) {
            return null;
        }
        // 通过content id获取content
        TTextContent tTextContent = contentMapper.selectTTextContentById(tQuestion.getContentId());
        BeanUtils.copyBeanProp(questionVo, tQuestion);
        // 将问题的内容转为List
        List<QuestionVo.Option> optionList = JSONArray.parseArray(tTextContent.getContent()).toList(QuestionVo.Option.class);
        // 将正确答案选项转为List
        List<String> correctArrays = JSONArray.parseArray(questionVo.getCorrect()).toList(String.class);
        questionVo.setCorrectArrays(correctArrays);
        questionVo.setContent(optionList);
        return questionVo;
    }

    /**
     * 查询题库列表
     *
     * @param tQuestion 题库
     * @return 题库
     */
    @Override
    public List<TQuestion> selectTQuestionList(TQuestion tQuestion)
    {
        return tQuestionMapper.selectTQuestionList(tQuestion);
    }

    @Override
    public List<QuestionVo> selectQuestionVoList(QuestionVo questionVo) {
        List<QuestionVo> questionVos = tQuestionMapper.selectQuestionVoList(questionVo);
        return questionVos.stream().peek(q -> {
            q.setContent(JSONArray.parseArray(q.getContentString()).toList(QuestionVo.Option.class));
            q.setCorrectArrays(JSONArray.parseArray(q.getCorrect()).toList(String.class));
        }).collect(Collectors.toList());
    }

    @Override
    public List<QuestionVo> selectUnChosenQuestionVoList(Long[] chosenIds) {
        List<QuestionVo> list = tQuestionMapper.selectUnChosenQuestionVoList(chosenIds);
        return list.stream().peek(q -> {
            q.setContent(JSONArray.parseArray(q.getContentString()).toList(QuestionVo.Option.class));
            q.setCorrectArrays(JSONArray.parseArray(q.getCorrect()).toList(String.class));
        }).collect(Collectors.toList());
    }

    /**
     * 新增题库
     *
     * @param tQuestion 题库
     * @return 结果
     */
    @Override
    public int insertTQuestion(TQuestion tQuestion)
    {
        return tQuestionMapper.insertTQuestion(tQuestion);
    }

    @Override
    @Transactional
    public int insertQuestionVo(QuestionVo questionVo) {
        TTextContent tTextContent = new TTextContent();
        TQuestion tQuestion = new TQuestion();
        BeanUtils.copyBeanProp(tQuestion, questionVo);
        // 获取前端的题干List
        List<QuestionVo.Option> content = questionVo.getContent();
        // 获取前端的正确答案List
        List<String> correctArrays = questionVo.getCorrectArrays();
        // 转为JSON
        tTextContent.setContent(JSONArray.toJSONString(content));
        tTextContent.setCreateTime(new Date());
        contentMapper.insertTTextContent(tTextContent);
        // mybatis设置了keyProperty之后, 自增的id会在实体类上
        Long id = tTextContent.getId();
        tQuestion.setCorrect(JSONArray.toJSONString(correctArrays));
        tQuestion.setContentId(id);
        // 设置CreateBy为当前用户id
        tQuestion.setCreatedBy(SecurityUtils.getUserId());
        return tQuestionMapper.insertTQuestion(tQuestion);
    }

    /**
     * 修改题库
     *
     * @param tQuestion 题库
     * @return 结果
     */
    @Override
    public int updateTQuestion(TQuestion tQuestion)
    {
        return tQuestionMapper.updateTQuestion(tQuestion);
    }

    @Override
    @Transactional
    public int updateQuestionVo(QuestionVo questionVo) {
        TTextContent tTextContent = new TTextContent();
        TQuestion tQuestion = new TQuestion();
        BeanUtils.copyBeanProp(tQuestion, questionVo);
        // 获取前端题干List
        List<QuestionVo.Option> content = questionVo.getContent();
        // 获取前端正确答案List
        List<String> correctArrays = questionVo.getCorrectArrays();
        // 转为JSON
        tTextContent.setId(questionVo.getContentId());
        tTextContent.setContent(JSONArray.toJSONString(content));
        contentMapper.updateTTextContent(tTextContent);
        tQuestion.setCorrect(JSONArray.toJSONString(correctArrays));
        return tQuestionMapper.updateTQuestion(tQuestion);
    }

    /**
     * 批量删除题库
     *
     * @param ids 需要删除的题库主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionByIds(Long[] ids)
    {
        return tQuestionMapper.deleteTQuestionByIds(ids);
    }

    /**
     * 删除题库信息
     *
     * @param id 题库主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionById(Long id)
    {
        return tQuestionMapper.deleteTQuestionById(id);
    }
}
